#ifndef TASK_POLLER_H
#define TASK_POLLER_H

#include "../reactor/Poller.h"

#include <vector>

struct pollfd;

class PollPoller : public Poller {
public:
    typedef std::vector<Channel *> ChannelList;

    explicit PollPoller(EventLoop *loop);

    ~PollPoller() override;

    /**
     * 调用poll去监听文件描述符事件
     * @param timeoutMs 超时时间
     * @param activeChannels 返回值列表, 监听到的文件描述符都会被封装到这个数组中
     * @return
     */
    Timestamp poll(int timeoutMs, ChannelList *activeChannels) override;

    /**
    * 负责维护和更新pollfds_数组,
    */
    void updateChannel(Channel *channel) override;

    void removeChannel(Channel *channel) override;

private:
    /**
    * 遍历pollfds_, 找出活动事件的文件描述符
    * 将对应活动事件的回调处理器填充到activeChannels中去, 这里numEvent就是我们要遍历的文件描述符数量
    * @param numEvents
    * @param activeChannels
    */
    void fillActiveChannels(int numEvents, ChannelList *activeChannels) const;

    typedef std::vector<struct pollfd> PollFdList;

    PollFdList pollfds_;
};

#endif //TASK_POLLER_H
